//
//  NativeFactory.swift
//  Runner
//
//  Created by longxiaobai on 2024/8/8.
//

import Foundation
import Flutter

class NativeFactory: NSObject, FlutterPlatformViewFactory {
    private var messenger: FlutterBinaryMessenger
    
    // 初始化方法，保存messager用于与Flutter通信
    init(messenger:FlutterBinaryMessenger) {
        self.messenger = messenger
        super.init()
    }
    
    // 创建平台视图的方法
    func create(withFrame frame: CGRect, viewIdentifier viewId: Int64, arguments args: Any?) -> FlutterPlatformView {
        if (args != nil) {
            print(args!);
        }
        print("==== FlutterPlatformViewFactory ====")
        // 返回自定义的平台视图实例
        return CustomView(frame: frame,viewIdentifier: viewId,arguments: args,binaryMessenger: messenger)
    }
    
    // 消毁平台视图的方法
    func destroy(_ view: FlutterPlatformView) {
        // 清理资源
    }
    
    func createArgsCodec() -> FlutterMessageCodec & NSObjectProtocol {
        return FlutterStandardMessageCodec.sharedInstance()
    }
}
